<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | AddonsBeanController v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace addons\common\controller;

use app\core\common\bean\BeanController;
use think\Config;
use think\Db;

abstract class AddonsBeanController extends AddonsController {
    use BeanController;

    /**
     * 获取某张表的查询对象
     *
     * @param bool $name
     * @return \think\db\Query
     * @throws \think\Exception
     */
    protected function _q($name = false) {
        if (!isset($name) || !$name) {
            $name = $this->_getTableName();
        } else {
            $name = $this->moduleName . "_" . $name;
        }
        $dbConfig = array_merge(Config::get("database"), $this->_getDbConfig());
        return Db::connect($dbConfig)->name($name);
    }

    /**
     * 获取表名
     *
     * @return string
     */
    protected function _getTableName() {
        $beanName = $this->_getBeanName();
        return empty($beanName) ? $this->moduleName : ($this->moduleName . "_" . $beanName);
    }
}